﻿namespace PropertyManagementSystemAPI.Common.TreeTable
{
    public class getRoleJurisdictionTreeList
    {
        /// <summary>
        /// 查询系统资源信息列表
        /// </summary>
        /// <returns></returns>
        public List<TreeList> GetSystemResourceModList(IFreeSql db, List<Models.Systemresourcemodule> list, int roleid)
        {

            //查询当前角色所拥有权限
            var rolelist = db.Select<Models.Roleresourcemodule>().Where(a => a.Roleid == roleid).ToList();

            List<TreeList> OrgList = new List<TreeList>();
            //利用组织机构名称进行条件查询

            //在全部数据中筛选出有子节点的父级数据
            var childlist = GetChildTreeList(list, new int());
            foreach (var item in childlist)
            {
                TreeList treelist = new TreeList();
                treelist.id = item.Id;
                treelist.code = item.ModuleCode;
                treelist.name = item.ModuleName;
                treelist.pid = item.ParentId;
                treelist.url = item.Url;
                treelist.icon = item.Icon;
                treelist.ELE_CHECKED = GetChecked(rolelist, item.Id).Count > 0 ? true : false;
                treelist.createdate = item.CreateDate;
                treelist.isParent = GetChildTreeList(list, item.Id).Count > 0 ? true : false;
                treelist.children = BindTree(list, rolelist, item.Id);
                OrgList.Add(treelist);
            }
            return OrgList;
        }

        /// <summary>
        /// 检测数据中是否含有子节点，有就返回含有子节点的对应数据
        /// </summary>
        /// <param name="list">现有数据</param>
        /// <param name="ID">父级顶点的ID值</param>
        /// <returns></returns>
        public List<Models.Systemresourcemodule> GetChildTreeList(List<Models.Systemresourcemodule> list, int? ID)
        {
            var childlist = list.Where(a => a.ParentId == ID).ToList();
            return childlist;
        }
        /// <summary>
        /// 利用递归实现查询子节点数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<object> BindTree(List<Models.Systemresourcemodule> list, List<Models.Roleresourcemodule> rolelist, int? ID)
        {
            List<object> objtreelist = new List<object>();
            var childlist = GetChildTreeList(list, ID);
            if (childlist.Count > 0)
            {
                foreach (var item in childlist)
                {
                    TreeList treelist = new TreeList();
                    treelist.id = item.Id;
                    treelist.code = item.ModuleCode;
                    treelist.name = item.ModuleName;
                    treelist.pid = item.ParentId;
                    treelist.url = item.Url;
                    treelist.icon = item.Icon;
                    treelist.ELE_CHECKED = GetChecked(rolelist, item.Id).Count > 0 ? true : false;
                    treelist.createdate = item.CreateDate;
                    treelist.isParent = GetChildTreeList(list, item.Id).Count > 0 ? true : false;
                    treelist.children = BindTree(list, rolelist, item.Id);
                    objtreelist.Add(treelist);
                }
            }
            return objtreelist;
        }


        /// <summary>
        /// 判断用户是否含有该权限，若有就返回对应条数
        /// </summary>
        /// <param name="list"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public List<Models.Roleresourcemodule> GetChecked(List<Models.Roleresourcemodule> list, int? roleid)
        {
            var tlist = list.Where(a => a.Resourcemoduleld == roleid).ToList();
            return tlist;
        }

    }
}
